if (SERVER) then
	AddCSLuaFile("shared.lua");
end;

if (CLIENT) then
	SWEP.PrintName			= "Pipe";
	SWEP.Instructions		 = "Left click to hit.";

	SWEP.Slot			= 2;
	SWEP.SlotPos		= 1;
	SWEP.ViewModelFlip 	= false;
	SWEP.DrawAmmo 		= false;
	SWEP.ViewModelFOV	= 62;
end;

SWEP.Base = "leaf_weapon_base";

SWEP.AdminSpawnable		= true;

SWEP.WorldModel 		= Model("models/props_canal/mattpipe.mdl");
SWEP.ViewModel			= Model("models/weapons/v_mattpip.mdl");

SWEP.Primary.ClipSize		= 1;
SWEP.Primary.DefaultClip	= 1;
SWEP.Primary.Automatic		= false;
SWEP.Primary.Ammo			= "none";

SWEP.Secondary.ClipSize		= 1;
SWEP.Secondary.DefaultClip	= 1;
SWEP.Secondary.Automatic	= false;
SWEP.Secondary.Ammo			= "none";

SWEP.HoldType 	 		= "melee";

SWEP.HolsteredPos 		= Vector(-2.4, -4, -12);
SWEP.HolsteredAngle		= Vector(0, 0, 0);

SWEP.hitSounds = {
	Sound("weapons/crowbar/crowbar_impact1.wav"),
	Sound("weapons/crowbar/crowbar_impact2.wav")
};

SWEP.hitPlayerSounds = {
	Sound("weapons/crossbow/hitbod1.wav"),
	Sound("weapons/crossbow/hitbod2.wav")
};

SWEP.swingSound = Sound("weapons/iceaxe/iceaxe_swing1.wav");

function SWEP:Reload()
end;

function SWEP:PrimaryAttack()
	self.Weapon:SetNextPrimaryFire(CurTime() +0.5);
	if (!self.Owner:GetNWBool("leaf_holstered")) then return; end;
	
	self.Owner:SetAnimation(PLAYER_ATTACK1);
	self.Weapon:SendWeaponAnim(ACT_VM_MISSCENTER);
	
	local tr = self.Owner:EyeTrace(90);
	
	if (tr.Hit) then
		local bullet = {}
		bullet.Num 		= 1
		bullet.Src 		= self.Owner:GetShootPos();	-- Source.
		bullet.Dir 		= self.Owner:GetAimVector();	-- Dir of bullet.
		bullet.Spread 	= Vector(0, 0, 0);			-- Aim Cone.
		bullet.Tracer	= 1;						-- Show a tracer on every x bullets.
		bullet.Force	= 50;						-- Amount of force to give to phys objects.
		bullet.Damage	= math.random(5, 10);
	
		self.Owner:FireBullets(bullet);
	end;
	
	if (SERVER) then
		if (tr.Entity:IsValid()) then
			local entity = tr.Entity;
			local norm = (entity:GetPos() -self.Owner:GetPos()):Normalize();
			local push = 8000 *norm;
			
			if (entity:IsPlayer()) then
				push = 100 *norm;
				entity:SetVelocity(push);
				
				if (entity:Health() < 25 and entity:Health() > 2) then
					entity:SpawnRagdoll();
					
					timer.Simple(26, function()
						if (IsValid(entity)) then
							entity:RemoveRagdoll(true, true);
							
							umsg.Start("leaf_unconscious", entity);
								umsg.Bool(false);
							umsg.End();
						end;
					end, entity);
				end;
				
				self.Weapon:SendWeaponAnim(ACT_VM_HITCENTER);
				self.Owner:EmitSound(self.hitPlayerSounds[math.random(1, 2)]);
			elseif (!entity:IsPlayer()) then
				self.Weapon:SendWeaponAnim(ACT_VM_HITCENTER);
				self.Owner:EmitSound(self.hitSounds[math.random(1, 2)]);
				
				local physics = entity:GetPhysicsObject();
				
				if (IsValid(physics)) then
					entity:GetPhysicsObject():ApplyForceOffset(push, tr.HitPos);
				else
					entity:SetVelocity(push);
				end;
			elseif (!entity and tr.Hit) then
				self.Weapon:SendWeaponAnim(ACT_VM_HITCENTER);
				self.Owner:EmitSound(self.hitSounds[math.random(1, 2)]);
			end;
		elseif (tr.Hit) then
			self.Weapon:SendWeaponAnim(ACT_VM_HITCENTER);
			self.Owner:EmitSound(self.hitSounds[math.random(1, 2)]);
		elseif (!tr.Hit) then
			self.Weapon:SendWeaponAnim(ACT_VM_MISSCENTER);
			self.Owner:EmitSound(self.swingSound);
		end;
	end;
end;

function SWEP:SecondaryAttack()
	self.Weapon:SetNextSecondaryFire(CurTime() +0.2);
	if (!self.Owner:GetNWBool("leaf_holstered")) then return; end;
	
	if (SERVER) then
		local tr = self.Owner:EyeTrace(80);
		
		if (IsValid(tr.Entity) and tr.Entity:IsDoor()) then
			self.Owner:EmitSound("physics/wood/wood_crate_impact_hard2.wav");
		end;
	end;
end;